//
//  SMTProgressWebView.m
//  goldenfinger
//
//  Created by apple on 15/12/23.
//  Copyright © 2017年 NewwingGroup. All rights reserved.
//

#import "SMTProgressWebView.h"
#import "NJKWebViewProgress.h"
#import "UIView+Utils.h"
@interface SMTProgressWebView ()<NJKWebViewProgressDelegate, UIWebViewDelegate> {
    UIWebView *_myWebview;
    NJKWebViewProgress *_progressProxy;
    
//    UIView *_progressview;
}
@end

@implementation SMTProgressWebView

- (instancetype)initWithFrame:(CGRect)frame andUrl:(NSString *)url {
    self = [self initWithFrame:frame];
    self.url = url;
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initViews];
        [self initData];
    }
    return self;
}

- (CGFloat)progress {
    return _progressProxy.progress;
}

- (void)reset {
    [_progressProxy reset];
}

- (void)initViews {
    self.backgroundColor = [UIColor whiteColor];
    
    _progressProxy = [[NJKWebViewProgress alloc] init];
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    _myWebview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height - 0)];
    _myWebview.backgroundColor = [UIColor whiteColor];
    _myWebview.delegate = _progressProxy;
    [self addSubview:_myWebview];
    
//    _progressview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 3)];
//    _progressview.backgroundColor = [UIColor redColor];
//    [self addSubview:_progressview];
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    _myWebview.size = frame.size;
}

- (void)initData {
    self.progressColor = [UIColor redColor];
}

- (void)dealloc {
    
}

//- (void)setProgressColor:(UIColor *)progressColor {
//    _progressview.backgroundColor = _progressColor = progressColor;
//}

- (void)setUrl:(NSString *)url{
    _url = [url copy];
    [_myWebview loadRequest:[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:url]]];
}

- (void)setWebViewDelegate:(id<UIWebViewDelegate>)webViewDelegate {
    _progressProxy.webViewProxyDelegate = _webViewDelegate = webViewDelegate;
}

- (UIWebView *)webView {
    return _myWebview;
}

- (void)setProgress:(float)progress animated:(BOOL)animated {
    BOOL isGrowing = progress > 0.0;
    [UIView animateWithDuration:(isGrowing && animated) ? 0.1 : 0.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//        _progressview.width = progress * self.width;
    } completion:nil];
    
    if (progress >= 1.0) {
        [UIView animateWithDuration:animated ? 0.3 : 0.0 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//            _progressview.alpha = 0.0;
        } completion:^(BOOL completed){
//            _progressview.width = 0;
        }];
    }
    else {
        [UIView animateWithDuration:animated ? 0.3 : 0.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//            _progressview.alpha = 1.0;
        } completion:nil];
    }
}

#pragma mark -delegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress {
    [self setProgress:progress animated:YES];
    if (progress >= 1.0 && [self.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
        _webTitle = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"].copy;
        [self.delegate webViewDidFinishLoad:self.webView];
    }
}
@end
